package xyz.icxl.kotlion.ext.view

import android.app.Activity
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.annotation.IdRes
import androidx.annotation.Px
import androidx.annotation.StringRes
import com.blankj.utilcode.util.KeyboardUtils

/**
 * 设置View的**Padding**值
 *
 * @param append 是否是追加（原先的基础上加上）
 * @param start 以像素为单位的左侧填充
 * @param top 以像素为单位的顶部填充
 * @param end 以像素为单位的右侧填充
 * @param bottom 以像素为单位的底部填充
 */
fun View?.setPadding(
    append: Boolean = false,
    @Px start: Int? = null,
    @Px top: Int? = null,
    @Px end: Int? = null,
    @Px bottom: Int? = null,
) {
    this?.setPadding(
        //Start
        if (null != start)
            if (append)
                paddingStart + start
            else
                start
        else paddingStart,
        //Top
        if (null != top)
            if (append)
                paddingTop + top
            else
                top
        else
            paddingTop,
        //End
        if (null != end)
            if (append)
                paddingEnd + end
            else
                end
        else
            paddingEnd,
        //Bottom
        if (null != bottom)
            if (append)
                paddingBottom + bottom
            else
                bottom
        else
            paddingBottom,
    )
}

/**
 * 设置View的**Margin**值
 *
 * @param append 是否是追加（原先的基础上加上）
 * @param start 以像素为单位的左侧填充
 * @param top 以像素为单位的顶部填充
 * @param end 以像素为单位的右侧填充
 * @param bottom 以像素为单位的底部填充
 */
fun View?.setMargin(
    append: Boolean = false,
    @Px start: Int? = null,
    @Px top: Int? = null,
    @Px end: Int? = null,
    @Px bottom: Int? = null,
) {
    (this?.layoutParams as? ViewGroup.MarginLayoutParams)?.apply {
        if (null!=start)
            if (append)
                leftMargin += start
            else
                leftMargin = start
        if (null!=top)
            if (append)
                topMargin += top
            else
                topMargin = top
        if (null!=end)
            if (append)
                rightMargin += end
            else
                rightMargin = end
        if (null!=bottom)
            if (append)
                bottomMargin += bottom
            else
                bottomMargin = bottom
        this@setMargin.layoutParams = this
    }
}

/**
 * 设置View高度
 */
fun View.setHeight(height:Int){
    layoutParams?.height = height
}


/**
 * 注册点击屏幕任意位置会收起键盘
 */
fun View.registerClickArbitraryHideKeyboard(activity: Activity){
    setOnClickListener {
        if (KeyboardUtils.isSoftInputVisible(activity)){
            KeyboardUtils.hideSoftInput(activity)
        }
    }
}


private var lastClickTime = 0L
private const val FAST_CLICK_DELAY_TIME = 500

/**
 * 安全的点击事件
 *
 * 此事件会防重复点击
 */
fun View.selfClickListener(listener:(View)->Unit){
    setOnClickListener {
        if (lastClickTime == 0L || (System.currentTimeMillis() - lastClickTime) >= FAST_CLICK_DELAY_TIME) {
            lastClickTime = System.currentTimeMillis()
            listener(it)
        }else{
            Log.d("selfClickListener","点击重复事件抛弃")
        }
    }
}

/**
 * 设置text
 */
fun View.withText(@IdRes id: Int,text:CharSequence):View{
    findViewById<TextView>(id).text = text
    return this
}

/**
 * 设置text
 */
fun View.withText(@IdRes id: Int,@StringRes strRes:Int):View{
    findViewById<TextView>(id).setText(strRes)
    visibility
    return this
}

/**
 * 设置`Visibility`
 * @param id 子View的Id
 * @param visibility [View.VISIBLE]、[View.INVISIBLE]、[View.GONE]
 */
fun View.withVisibility(@IdRes id:Int,visibility:Int):View{
    findViewById<View>(id).visibility = visibility
    return this
}

fun View.withSelfClickListener(@IdRes id: Int,listener: (View) -> Unit):View{
    findViewById<View>(id).selfClickListener(listener)
    return this
}
